<?php

date_default_timezone_set('Europe/Paris');

include('includes/connexion.php');
$adminmanager = new AdminManager($connexion);
$elevemanager = new EleveManager($connexion);
session_start();
include('includes/secu.php');

$accesmanager = new AccesManager($connexion);
$collegemanager = new CollegeManager($connexion);
$classemanager = new ClasseManager($connexion);
$testmanager = new TestManager($connexion);


if(isset($_POST['vformacces'])){
	$newacces = new Acces();
	$newacces->setIdRefTest($_POST['test']);
	$newacces->setIdRefClasse($_POST['classe']);
	$newacces->setDateacces(mktime($_POST['heure'], $_POST['minute'], 0, $_POST['mois'], $_POST['jour'], $_POST['annee']));
	$accesmanager->add($newacces);
}

if(isset($_POST['mformadmin'])){
	if($_POST['college'] == 0)
		$_POST['college'] = NULL;
	
	$madmin = new Admin();
	$madmin->setId($_POST['id']);
	$madmin->setPrenom($_POST['prenom']);
	$madmin->setNom($_POST['nom']);
	$madmin->setIdRefCollege($_POST['college']);
	$adminmanager->update($madmin);
}

if(isset($_POST['vformadmin'])){
	if($_POST['college'] == 0)
		$_POST['college'] = NULL;
	
	$newadmin = new Admin();
	$newadmin->setPrenom($_POST['prenom']);
	$newadmin->setNom($_POST['nom']);
	$newadmin->setIdRefCollege($_POST['college']);
	$newadmin->setPassword(hash('sha256', $_POST['password']));
	$adminmanager->add($newadmin);
}

if(isset($_GET['type'])){
	if($_GET['type']=='ac')
		$accesmanager->delete($accesmanager->get($_GET['id']));	
	if($_GET['type']=='ad')
		$adminmanager->delete($adminmanager->get($_GET['id']));	
}
?>
<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <title>Tables Advanced - Target Admin</title>

  <meta charset="utf-8">
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width">

  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,700italic,400,600,700">
  <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,300,700">
  <link rel="stylesheet" href="./css/font-awesome.min.css">
  <link rel="stylesheet" href="./js/libs/css/ui-lightness/jquery-ui-1.9.2.custom.min.css">
  <link rel="stylesheet" href="./css/bootstrap.min.css">

  <!-- Plugin CSS -->
  <link rel="stylesheet" href="./js/plugins/icheck/skins/minimal/blue.css">
  <link rel="stylesheet" href="js/plugins/select2/select2.css">
  <link rel="stylesheet" href="js/plugins/datepicker/datepicker.css">
  <link rel="stylesheet" href="js/plugins/simplecolorpicker/jquery.simplecolorpicker.css">
  <link rel="stylesheet" href="js/plugins/timepicker/bootstrap-timepicker.css">
  <link rel="stylesheet" href="js/plugins/fileupload/bootstrap-fileupload.css">
  <link rel="stylesheet" href="js/plugins/magnific/magnific-popup.css">
  <link rel="stylesheet" href="js/libs/css/ui-lightness/jquery-ui-1.9.2.custom.css">
  
  <!-- App CSS -->
  <link rel="stylesheet" href="css/target-admin.css">
  <link rel="stylesheet" href="css/custom.css">

  <!-- Page CSS -->
  <link rel="stylesheet" href="css/demos/ui-notifications.css">


  <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
  <![endif]-->
</head>

<body>

<?php include('includes/navbar.php'); ?>

<?php include('includes/mainbar.php'); ?>

<div class="container">

  <div class="content">

    <div class="content-container">

      

      <div class="content-header">
        <h2 class="content-header-title">Liste des accès</h2>
      </div> <!-- /.content-header -->

      

      <div class="row">

        <div class="col-md-12">

          <div class="portlet">

            <div class="portlet-header">

              <h3>
                <i class="fa fa-table"></i>
                Liste des accès aux tests
              </h3>

            </div> <!-- /.portlet-header -->

            <div class="portlet-content">           

              <div class="table-responsive">

              <table 
                class="table table-striped table-bordered table-hover table-highlight table-checkable" 
                data-provide="datatable" 
                data-display-rows="10"
                data-info="true"
                data-search="true"
                data-length-change="true"
                data-paginate="true"
              >
                  <thead>
                    <tr>
                      <th class="col-lg-1"></th>
                      <th data-filterable="true" data-sortable="true">Intitulé du test</th>
                      <th data-filterable="true" data-sortable="true">Classe</th>
                      <th data-filterable="true" data-sortable="true" data-direction="desc">Date d'accès</th>
                    </tr>
                  </thead>
                  <tbody>
<?php foreach($accesmanager->getList() as $key => $value){ ?>
                    <tr id="row<?php echo $value->id(); ?>">
                      <td class="checkbox-column"><a href="acces.php?type=ac&id=<?php echo $value->id(); ?>" style='color:red;' onclick="return confirm('Etes-vous sur de vouloir supprimer cet accès ?');" class="fa fa-trash-o"></a></td>
                      <td class="test<?php echo $value->id(); ?>test<?php echo $value->idRefTest(); ?>"><?php echo $testmanager->get($value->idRefTest())->intitule()." (".$testmanager->get($value->idRefTest())->duree()." heure(s))";?></td>
                      <td class="classe<?php echo $value->id(); ?>classe<?php echo $value->idRefClasse(); ?>"><?php echo $collegemanager->get($classemanager->get($value->idRefClasse())->idRefCollege())->nom()." - ".$classemanager->get($value->idRefClasse())->nom();?></td>
                      <td class="date<?php echo $value->id(); ?>"><?php if(time() > $value->dateacces()){ echo "<font color='#00FF00'>".date('d/m/Y à H:i', $value->dateacces())."</font>"; } else { echo "<font color='#FF0000'>".date('d/m/Y à H:i', $value->dateacces())."</font>"; }?></td>
                    </tr>
<?php } ?>
                  </tbody>
                </table>
              </div> <!-- /.table-responsive -->
            </div> <!-- /.portlet-content -->     
		</div>
        </div> <!-- /.col -->

      </div> <!-- /.row -->

<div class="row">

        <div class="col-md-12">

          <div class="portlet">

            <div class="portlet-header">

              <h3>
                <i class="fa fa-table"></i>
                Liste des accès aux résultats
              </h3>

            </div> <!-- /.portlet-header -->

            <div class="portlet-content">           

              <div class="table-responsive">

              <table 
                class="table table-striped table-bordered table-hover table-highlight table-checkable" 
                data-provide="datatable" 
                data-display-rows="10"
                data-info="true"
                data-search="true"
                data-length-change="true"
                data-paginate="true"
              >
                  <thead>
                    <tr>
                      <th class="col-lg-1"></th>
                      <th data-filterable="true" data-sortable="true" data-direction="desc">Nom</th>
                      <th data-filterable="true" data-sortable="true">Prénom</th>
                      <th data-filterable="true" data-sortable="true">Collège</th>
                    </tr>
                  </thead>
                  <tbody>
<?php foreach($adminmanager->getList() as $key => $value){ ?>
                    <tr id="2row<?php echo $value->id(); ?>">
                      <td class="checkbox-column"><a data-toggle="modal" href="#ModifAccesResult" class="fa fa-gear" id="2gear<?php echo $value->id(); ?>"></a> <a href="acces.php?type=ad&id=<?php echo $value->id(); ?>" style='color:red;' onclick="return confirm('Etes vous sur de vouloir supprimer cet administrateur?');"><span class="fa fa-trash-o"></span></a></td>
                      <td class="nom<?php echo $value->id(); ?>"><?php echo $value->nom();?></td>
                      <td class="prenom<?php echo $value->id(); ?>"><?php echo $value->prenom();?></td>
                      <td class="college<?php echo $value->id(); ?>college<?php echo $value->idRefCollege(); ?>"><?php if($value->idRefCollege()==0){echo "<b>Tous</b>";}else{ echo $collegemanager->get($value->idRefCollege())->nom();}?></td>
                    </tr>
<?php } ?>
                  </tbody>
                </table>
              </div> <!-- /.table-responsive -->
              <form action="acces.php" id="formadmin" method="post" class="form parsley-form">
              <div class="col-md-2"><label for="nom">Nom</label><input type="text" name="nom" size="15"/></div>
              <div class="col-md-2"><label for="prenom">Prenom</label><input type="text" name="prenom" size="15"/></div>
              <div class="col-md-3">
              <label for="college">Collège</label><select name="college" class="form-control">
                    <option value="0">Tous</option>
<?php foreach($collegemanager->getList() as $key => $value){ ?>
                    <option value="<?php echo $value->id(); ?>"><?php echo $value->nom(); ?></option>
<?php } ?>
                  </select>
              </div>
              <div class="col-md-3"><label for="password">Password</label><input type="password" name="password"/></div>
              <div class="col-md-2">
              <br><input type="submit" value="Valider" name="vformadmin" class="btn btn-default"/>
              </div>
                  </form>

            </div> <!-- /.portlet-content -->     
		</div>
        </div> <!-- /.col -->

      </div>

    </div> <!-- /.content-container -->
      
  </div> <!-- /.content -->

</div> <!-- /.container -->


<?php include('includes/footer.php'); ?>

  <script src="./js/libs/jquery-1.10.1.min.js"></script>
  <script src="./js/libs/jquery-ui-1.9.2.custom.min.js"></script>
  <script src="./js/libs/bootstrap.min.js"></script>

  <!--[if lt IE 9]>
  <script src="./js/libs/excanvas.compiled.js"></script>
  <![endif]-->
  
  <!-- Plugin JS -->
  <script src="./js/plugins/datatables/jquery.dataTables.min.js"></script>
  <script src="./js/plugins/datatables/DT_bootstrap.js"></script>
  <script src="./js/plugins/tableCheckable/jquery.tableCheckable.js"></script>
  <script src="./js/plugins/icheck/jquery.icheck.min.js"></script>

  <!-- App JS -->
  <script src="./js/target-admin.js"></script>

<div id="ModifAccesResult" class="modal modal-styled fade">
  <div class="modal-dialog">
    <div class="modal-content">
    <form action="acces.php" id="mformadmin" method="post" class="form parsley-form">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3 class="modal-title">Modification de l'accès aux résultats</h3>
      </div>
      <div class="modal-body">
      	<div class="row">
              <div class="col-md-6">
              <label for="nom">Nom</label><input type="text" name="nom"/>
              </div>
              <div class="col-md-6">
              <label for="prenom">Prénom</label><input type="text" name="prenom"/>
              </div>
          </div>
      	<div class="row">
              <div class="col-md-6">
              <label for="college">Collège</label><select name="college" class="form-control">
                    <option value="0">Tous</option>
    <?php foreach($collegemanager->getList() as $key => $value){ ?>
                    <option value="<?php echo $value->id(); ?>"><?php echo $value->nom(); ?></option>
    <?php } ?>
                  </select>
              </div>
          </div>
      </div>
      <div class="modal-footer">
      	<input type="text" name="id" hidden/>
        <button type="button" class="btn btn-tertiary" data-dismiss="modal">Fermer</button>
        <input type="submit" class="btn btn-primary" name="mformadmin" value="Sauvegarder"/>
      </div>
      </form>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<script type="text/javascript">
$(document).ready(function() {
	$("[id^='gear']").click(function() {
		var arr = $(this).attr("id").split("gear");
		
		$("#mformacces input[name=id]").val(arr[1]);
		
		var rowtest = "#row"+arr[1]+" [class^='test']"; 
		var arr2 = $(rowtest).attr("class").split("test");
		$("#mformacces select[name=test]").val(arr2[2].split(' ')[0]);
		
		var rowclasse = "#row"+arr[1]+" [class^='classe']";
		var arr3 = $(rowclasse).attr("class").split("classe");
		$("#mformacces select[name=classe]").val(arr3[2].split(' ')[0]);
		
		var classDate = ".date" + arr[1];
		var reg = new RegExp("([0-9]+)/([0-9]+)/([0-9]+) à ([0-9]+):([0-9]+)");
		var date = $(classDate).html().split(reg);

		$("#mformacces input[name=jour]").val(date[1]);
		$("#mformacces input[name=mois]").val(date[2]);
		$("#mformacces input[name=annee]").val(date[3]);
		$("#mformacces input[name=heure]").val(date[4]);
		$("#mformacces input[name=minute]").val(date[5]);
	});
});
</script>

<script type="text/javascript">
$(document).ready(function() {
	$("[id^='2gear']").click(function() {
		var arr = $(this).attr("id").split("2gear");
		
		$("#mformadmin input[name=id]").val(arr[1]);
		
		var classNom = ".nom" + arr[1];
		$("#mformadmin input[name=nom]").val($(classNom).html());
		
		var classPrenom = ".prenom" + arr[1];
		$("#mformadmin input[name=prenom]").val($(classPrenom).html());		
		
		var rowcollege = "#2row"+arr[1]+" [class^='college']";
		var arr3 = $(rowcollege).attr("class").split("college");
		$("#mformadmin select[name=college]").val(arr3[2].split(' ')[0]);
	});
});
</script>
  
</body>
</html>